package com.zzh.myapplication.manager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.icarplus.car.R;
import net.icarplus.car.tools.DialogUtils;
import net.icarplus.car.tools.PromptDialog;

/**
 * 项目名称：icarplus-car-android
 * 类描述：
 * 创建人：Gophy
 * 创建时间：16/3/11 15:35
 * 修改人：Gophy
 * 修改时间：16/3/11 15:35
 * 修改备注：
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener{

    private PromptDialog alertDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        getsavedInstanceState(savedInstanceState);
        View view = onCreateView(inflater, container);
        initComponent(view);
        setListener();
        onAfterInit();
        return view;
    }

    /**
     * 获取保存数据
     * @param savedInstanceState
     */
    public abstract void getsavedInstanceState(Bundle savedInstanceState);

    /**
     * 设置显示页面
     */
    public abstract View onCreateView(LayoutInflater inflater, ViewGroup container);

    /**
     * 初始化界面控件
     * @param view 控件所在View
     */
    public abstract void initComponent(View view);

    /**
     * 各种事件
     */
    public abstract void setListener();

    /**
     * 最后要做的
     */
    public abstract void onAfterInit();

    /**
     * 打开Activity, 伴随动画
     * @param intent
     */
    public void start_Activity(Intent intent) {
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    /**
     * 打开Activity, 伴随动画
     * @param intent
     */
    public void start_ActivityForResult(Intent intent, int requestCode) {
        startActivityForResult(intent, requestCode);
        getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    public void finish_Activity() {
        getActivity().finish();
        getActivity().overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    /**
     * 显示对话框
     * @param title
     * @param message
     * @param positiveListener
     * @param negativeListener
     */
    public void showAlerDialog(String title, String message,
                               PromptDialog.OnClickListener positiveListener, PromptDialog.OnClickListener negativeListener) {
        alertDialog = DialogUtils.getConfirmDialog(getActivity(), title, message, positiveListener, negativeListener);
    }

    /**
     * 显示对话框
     * @param title
     * @param message
     * @param positiveListener
     * @param negativeListener
     */

    public void showAlerDialog(String title, String message, PromptDialog.OnClickListener positiveListener) {
        alertDialog = DialogUtils.getDefaultDialog(getActivity(), title, message, positiveListener);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if(alertDialog != null) {
            alertDialog.dismiss();
            alertDialog = null;
        }
    }
}
